<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文件下载器</title>
</head>
<body>
  <script type="text/javascript">
    const writableStream = new WritableStream({
      // 构造函数被调用时调用
      start(controller) {
        console.log('writer start--->');
      },
      // 每当读取一个数据块时触发
      write(chunk, controller) {
        return new Promise(resolve => {
          setTimeout(() => {
            console.log('writer chunk--->', chunk)
            resolve()
          }, 1000);
        })
      },
      // 当完成全部数据的接收时触发
      close(controller) {
        console.log('writer close--->')
      },
      // 终止流的写入时触发
      abort(reason) {
        console.log('writer abort--->')
      }
    })
    const writeLoad = async () => {
      const writer = writableStream.getWriter();
      for (const chunk of '这是一段数据') {
        await writer.ready
        await writer.write(chunk)
      }
      await writer.close()
    }
    writeLoad()
  </script>
</body>
</html>